﻿using System;
using System.IO;
using System.Linq;

namespace FileMover
{
    class Program
    {
        static void Main(string[] args)
        {
            var config = ConfigUtils.GetConfig<ConfigModel>("app.json");
            config.Exts = config.Exts.Select(a => a.ToLower()).ToArray();

            //根据扩展名取出所有符合的文件
            var files = new DirectoryInfo(config.SourcePath).GetFiles("*.*", SearchOption.AllDirectories);
            files = files.Where(a => config.Exts.Contains(a.Extension.ToLower())).ToArray();

            foreach (var item in files)
            {
                try
                {
                    var targetFilePath = Path.Join(config.TargetPath, item.Name);
                    if (!new FileInfo(targetFilePath).Exists)
                    {
                        item.MoveTo(targetFilePath, false);
                    }
                    else if (config.MoveMode == "overwrite")
                    {
                        item.MoveTo(targetFilePath, true);
                    }
                    else if (config.MoveMode == "rename")
                    {
                        var newName = $"{item.Name.Replace(item.Extension, string.Empty)} - copy{item.Extension}";
                        targetFilePath = Path.Join(config.TargetPath, newName);
                        item.MoveTo(targetFilePath, true);
                    }
                    Console.WriteLine("{0} => {1}", item.FullName, targetFilePath);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }

            Console.WriteLine("task is complete!");
            Console.ReadLine();
        }
    }
}
